﻿using System;
using System.Collections;
using System.Reflection;
using System.Xml;
using TextEditor.TextDocument.Document;

namespace TextEditor
{
   

    public class ZYTextSaveLogCollection
    {
        private ZYTextSaveLog myCurrentSaveLog = new ZYTextSaveLog();
        private ArrayList myList = new ArrayList();
        private ZYTextDocument myOwnerDocument = null;

        public ZYTextSaveLogCollection()
        {
            this.myList.Add(this.myCurrentSaveLog);
        }

        public void AddNewSaveLogs()
        {
            ZYTextSaveLog savelog = new ZYTextSaveLog();
            this.myCurrentSaveLog = savelog;
            this.myList.Add(savelog);
        }

        public void Clear()
        {
            this.myList.Clear();
            this.myCurrentSaveLog.MarkLevel = 0;
            this.myList.Add(this.myCurrentSaveLog);
        }

        public bool FromXML(XmlElement RootElement)
        {
            this.myList.Clear();
            if (RootElement != null)
            {
                foreach (XmlNode node in RootElement.ChildNodes)
                {
                    if (node is XmlElement)
                    {
                        ZYTextSaveLog log = new ZYTextSaveLog();
                        log.FromXML(node as XmlElement);
                        log.OwnerDocument = this.myOwnerDocument;
                        this.myList.Add(log);
                    }
                }
                this.myList.Add(this.myCurrentSaveLog);
                return true;
            }
            this.myList.Add(this.myCurrentSaveLog);
            return false;
        }

        public string GetXMLName()
        {
            return "savelogs";
        }

        public bool IndexAvailable(int Index)
        {
            return ((Index >= 0) && (Index < this.myList.Count));
        }

        public string[] ToStringArray()
        {
            string[] strArray = new string[this.myList.Count];
            int index = 0;
            foreach (ZYTextSaveLog log in this.myList)
            {
                strArray[index] = log.DisplayText();
                index++;
            }
            return strArray;
        }

        public bool ToXML(XmlElement RootElement)
        {
            if (RootElement != null)
            {
                foreach (ZYTextSaveLog log in this.myList)
                {
                    XmlElement newChild = RootElement.OwnerDocument.CreateElement(log.GetXMLName());
                    RootElement.AppendChild(newChild);
                    log.ToXML(newChild);
                }
                return true;
            }
            return false;
        }

        public int Count
        {
            get
            {
                return this.myList.Count;
            }
        }

        public int CurrentIndex
        {
            get
            {
                return (this.myList.Count - 1);
            }
        }

        public ZYTextSaveLog CurrentSaveLog
        {
            get
            {
                return this.myCurrentSaveLog;
            }
            set
            {
                this.myCurrentSaveLog = value;
            }
        }

        public string CurrentUserName
        {
            get
            {
                return this.myCurrentSaveLog.UserName;
            }
            set
            {
                this.myCurrentSaveLog.UserName = value;
                if ((this.myOwnerDocument != null) && (this.myOwnerDocument.DocumentElement != null))
                {
                    this.myOwnerDocument.UpdateUserName();
                }
            }
        }

        public ZYTextSaveLog this[int index]
        {
            get
            {
                return (ZYTextSaveLog) this.myList[index];
            }
        }

        public ArrayList List
        {
            get
            {
                return this.myList;
            }
        }

        public ZYTextDocument OwnerDocument
        {
            get
            {
                return this.myOwnerDocument;
            }
            set
            {
                this.myOwnerDocument = value;
                foreach (ZYTextSaveLog log in this.myList)
                {
                    log.OwnerDocument = this.myOwnerDocument;
                }
                this.myCurrentSaveLog.OwnerDocument = this.myOwnerDocument;
            }
        }

        public ZYTextSaveLog GetSaveLog(int index)
        {
            if (!this.IndexAvailable(index))
            {
                if (index >= this.myList.Count)
                {
                    index = this.myList.Count - 2;
                    if (index < 0)
                        index = 0;
                }
            }
            return this[index];
        }

    }
}

